package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.CustomerInfoBeanParser;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.NewCustomerInfoBean;
import cn.com.ghy.android.bean.NewCustomerInfoBeanParser;
import cn.com.ghy.android.bean.YeTaiTypeBean;
import cn.com.ghy.android.bean.YeTaiTypeBeanParser;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:客户信息服务器
 * @author wuz  @date 2011-5-4 13:25:34
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 * @commonts 2011-6-2 08:22:53 wuz 修改,合并分散的客户信息服务类
 */

public class CustOpeService {
	private String url = CommonStatic.URL_HEAD + "CustOpe_Ar.asmx";
	private String methodName = "";
	private Map<String, String> params = null;
	
	private static final String STRXML = "strxml";
	private static final String PERNR = "pernr";
	private static final String SAOF = "saof";
	private static final String PARTNER = "partner";
	private static final String BASEINFO = "baseinfo";
	
	public List<CustomerInfoBean> GetCustList(String customerNum, 
			String customerName, LoginInfoBean loginInfo) throws Exception {
		methodName = "GetCustList";
		params = new HashMap<String, String>();
		StringWriter writer = new StringWriter();
		String strxml = CustomerInfoBeanParser.createXML(customerNum, customerName, writer);
		writer.close();
		strxml = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strxml.getBytes()));
		String pernr = loginInfo.getPernr();
		String saof = loginInfo.getSaof();
		params.put(STRXML, strxml);
		params.put(PERNR, pernr);
		params.put(SAOF, saof);
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CustomerInfoBean> list = CustomerInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}
	
	public List<CustomerInfoBean> GetCustShow(String partner) throws Exception {
		methodName = "GetCustShow";
		params = new HashMap<String, String>();
		params.put(PARTNER, partner);
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<CustomerInfoBean> list = CustomerInfoBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}
	
	public List<YeTaiTypeBean> GetYt() throws Exception {
		methodName = "GetYt";
		params = new HashMap<String, String>();
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<YeTaiTypeBean> list = YeTaiTypeBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}
	
	public String AddNewCust(NewCustomerInfoBean bean, LoginInfoBean loginInfo) throws Exception {
		methodName = "AddNewCust";
		params = new HashMap<String, String>();
		StringWriter writer = new StringWriter();
		String strxml = NewCustomerInfoBeanParser.createXML(bean, loginInfo, writer);
		writer.close();
		strxml = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(strxml.getBytes()));
		params.put(BASEINFO, strxml);
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		return result;
	}
}
